<template>
	<nt-container bg-color="#F2F4F6">
		<view class="nt-recovery">
			<view class="nt-recovery-header">
				<view class="nt-recovery-header__left">
					<view class="nt-recovery-header__left--top">
						{{ orderInfo.mtype }} <text>{{ orderInfo.customerphone | mobilePrivacyFilter }}</text>
					</view>
					<view class="nt-recovery-header__left--addr">
						{{ orderInfo.area }}{{ orderInfo.district }}{{ orderInfo.address }}
					</view>
				</view>
				<view class="nt-recovery-header__right"
					@tap="nav('/subpackage/order/pages/recyclingDetail/index?id='+orderInfo.id)">回收</view>
			</view>
			<view class="nt-recovery-form">
				<view class="nt-recovery-form__table">
					<view class="nt-recovery-form__row">
						<view class="nt-recovery-form__cell title width_400">维修内容</view>
						<view class="nt-recovery-form__cell title width_200">总价</view>
						<view class="nt-recovery-form__cell title width_100"></view>
					</view>
					<view class="nt-recovery-form__row" v-for="(it, index) in quotationList" :key="index">
						<view class="nt-recovery-form__cell width_400">
							<input type="text" v-model="it.content" />
						</view>
						<view class="nt-recovery-form__cell width_200">
							<input type="text" v-model="it.price" />
						</view>
						<view class="nt-recovery-form__cell width_100" @tap="addQuotationsInfo(index)">
							<view class="nt-recovery-form__btn" :class="{ decrease: index > 0 }">
								{{ index > 0 ? "-" : "+" }}
							</view>
						</view>
					</view>
				</view>
				<view class="nt-recovery-quotation">
					总报价：<text>{{quotation}}</text>
				</view>
			</view>
		</view>
		<view class="nt-order-detail__remarkswarp">
			<view class="nt-order-detail__remarks">
				<view class="nt-order-detail__remarks--label">师傅备注：</view>
				<textarea class="nt-order-detail__remarks--textarea" placeholder="请填写师傅备注"
					v-model="fix_content"></textarea>
			</view>
		</view>

		<view class="scan-code-footer">
			<template v-if="!isUpdate">
				<view class="scan-code-footer__ignore" @tap="modalOptions.show = true">带回</view>
				<view class="scan-code-footer__next" @tap="submit">下一步</view>
			</template>
			<view class="scan-code-footer__next" @tap="submit" v-else>确认修改</view>
		</view>

		<u-modal :show="modalOptions.show" :title="modalOptions.title" :content='modalOptions.content'
			:asyncClose="true" showCancelButton @confirm="selfBringBack" @cancel="modalOptions.show = false"></u-modal>
	</nt-container>
</template>

<script>
	import {
		addQuotation,
		applyBringBack,
		getOrderDetail
	} from "@/api"
	export default {
		data() {
			return {
				quotationList: [{
					content: "",
					price: ""
				}],
				orderId: "",
				modalOptions: {
					show: false,
					content: "确定带回次订单？"
				},
				fix_content: "",
				orderInfo: {},
				isUpdate: false
			}
		},
		computed: {
			quotation() {
				const result = this.quotationList.reduce((acc, item) => acc += Number(item.price), 0)
				return !isNaN(result) ? result.toFixed(2) : "请填写正确的价格"
			}
		},
		onLoad(options) {
			this.orderId = options.id
			this.getDetail()
		},
		methods: {
			addQuotationsInfo(index) {
				// 删除
				if (index > 0) {
					this.quotationList.splice(index, 1)
					return;
				}
				const tpl = {
					content: "",
					price: ""
				};
				this.$set(this.quotationList, this.quotationList.length, tpl)
			},
			// 本人带回
			selfBringBack(e) {
				console.log(e)
				applyBringBack(this.orderId).then(res => {
					const {
						code,
						data,
						msg
					} = res;
					if (code === 1) {
						this.toast(msg)
						this.modalOptions.show = false;
						this.$store.commit("app/SET_ORDER_TYPE_INDEX", 2)
						setTimeout(() => {
							uni.$emit('changeList')
							this.swi("/pages/staging/index")
						}, 1000)
					}
				})
			},
			// 提交
			submit() {

				let data = this.quotationList.filter(item => !!item.content && !!item.price)
				if (!data.length) return this.toast("请填写维修内容")
				addQuotation({
					realorder_id: this.orderId,
					quote: data,
					fix_content: this.fix_content
				}).then(res => {
					if (res.code == 1) {
						this.toast("提交成功")
						this.$store.commit("app/SET_ORDER_TYPE_INDEX", 2)
						setTimeout(() => {
							if (this.orderInfo.quote.length === 0) {
								this.rep(`/subpackage/order/pages/payment/index?id=${this.orderId}`)
							} else {
								this.swi("/pages/staging/index")
							}
							uni.$emit('changeList')
						}, 1000)
					}
				})
			},
			// 订单详情
			getDetail() {
				getOrderDetail(this.orderId).then(res => {
					const {
						data,
						code
					} = res;
					this.orderInfo = data;
					this.isUpdate = data.quote.length > 0
					if (data.quote.length > 0) {
						this.quotationList = data.quote.map(item => {
							return {
								content: item.content,
								price: item.price
							}
						})
					}
					this.fix_content = data.problem
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.nt-recovery {
		&-header {
			height: 160rpx;
			background: var(--theme-color);
			padding: 0 40rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			color: #fff;

			&__left {
				&--top {
					font-weight: bold;
					font-size: 36rpx;

					text {
						font-size: 28rpx;
						margin-left: 20rpx;
						font-weight: normal
					}
				}

				&--addr {
					font-size: 28rpx;
					margin-top: 10rpx;
				}

			}

			&__right {
				width: 125rpx;
				height: 70rpx;
				line-height: 70rpx;
				border-radius: 10rpx;
				background-color: #22D58D;
				font-size: 36rpx;
				font-weight: bold;
				text-align: center;
			}
		}

		&-quotation {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;

			text {
				color: #f00
			}
		}

		&-form {
			padding: 20rpx;
			box-sizing: border-box;
			background-color: #fff;

			.title {
				font-weight: bold;
				color: #333;
				font-size: 32rpx;
			}

			input {
				height: 60rpx;
				border-radius: 10rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				background: #F2F4F6;
				margin: 0 auto;
				text-align: left;
			}

			&__table {
				display: table;
				width: 100%;
			}

			&__row {
				// display: table-row
				display: flex;
				align-items: center;
			}

			&__cell {
				text-align: center;
				// display: table-cell;
				padding: 20rpx 10rpx;

				&.width_400 {
					width: 360rpx;
				}

				&.width_200 {
					width: 200rpx;
				}

				&.width_100 {
					width: 100rpx;
				}
			}

			&__btn {
				width: 50rpx;
				height: 50rpx;
				line-height: 50rpx;
				border-radius: 50%;
				text-align: center;
				font-weight: bold;
				background: var(--theme-color);
				margin: auto;
				position: relative;
				// top: 10rpx;
				color: #fff;
				font-size: 50rpx;
				vertical-align: middle;

				&.decrease {
					background: #000;
				}
			}


		}
	}

	.nt-order-detail {
		&__remarkswarp {
			margin-top: 30rpx;
			padding: 25rpx 20rpx;
			background: #fff;
			box-sizing: border-box;
			margin-bottom: 30rpx;
		}

		&__remarks {
			border-radius: 20rpx;
			border: 1rpx solid #F8D4B9;
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;

			&--label {
				font-size: 28rpx;
				color: #333;
				font-weight: bold;
				white-space: nowrap;
				margin-right: 10rpx;
			}

			&--textarea {
				heigh: 160rpx;
				width: 100%;
			}
		}
	}

	.scan-code {
		&-footer {
			height: 130rpx;
			line-height: 130rpx;
			display: flex;
			align-items: center;
			text-align: center;
			font-size: 36rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			font-weight: bold;

			&__ignore {
				background: #fff;
				height: 100%;
				color: var(--theme-color);
				width: 225rpx;
			}

			&__next {
				background: var(--theme-color);
				color: #fff;
				height: 100%;
				flex: 1;
			}
		}
	}
</style>